<?php
class item_mystery extends template_item
{
	var $default_name = "Get A Mystery Thing";
	var $default_description = "Get a any item or get some money. Who knows?";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['percent'] = 50;
			$this->item_config['lowest'] = 0;
			$this->item_config['highest'] = 1000;
			$this->item_config['items'] = array();
			$this->item_config['deduct'] = 1;
		}
	}
	
	function admin_form()
	{
		$items = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			$null = array();
			if(!$this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($null))
			{
				continue;
			}
			$text = $item['item_name']." (Stock: ";
			$text .= $this->ipsclass->numbers->compare($item['stock'],0) < 0 ? 'Infinite' : $item['stock'];
			$text .= $item['hide_item'] ? ", Hidden)" : ")";
			$items[] = array($item_id, $text);
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Minimum amount of money</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][item_lowest]", $this->item_config['lowest']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Maximum amount of money</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][item_highest]", $this->item_config['highest']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which Items should be randomly picked</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][items][]", $items, $this->item_config['items']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Deduct item from stock?</b><br />At the time when this item is used, it will not pick out items with no stock",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][deduct]", $this->item_config['deduct']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What percent chance to get money?</b><br />The other half of percentage will to go items.",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][percent]", $this->item_config['percent'])."%",
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['percent'] = $this->ipsclass->numbers->max($this->ipsclass->numbers->min($this->ipsclass->numbers->parse_input_number($input['percent']),100),0);
		$save['lowest'] = $this->ipsclass->numbers->parse_input_number($input['item_lowest'],'int');
		$save['highest'] = $this->ipsclass->numbers->parse_input_number($input['item_highest'],'int');
		$save['items'] = array();
		if(is_array($input['items']) && count($input['items']))
		{
			foreach($input['items'] as $item_id)
			{
				$save['items'][] = $item_id;
			}
		}
		$save['deduct'] = intval($input['deduct']) & 1;
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$items_to_pick = $this->item_config['items'];
		$stock = array();
		$null = array();
		foreach($items_to_pick as $i => $item_id)
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item_id]) || !$this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($null) || ($this->item_config['deduct'] && $this->ipsclass->cache['market_items'][$item_id]['stock'] == 0))
			{
				unset($items_to_pick[$i]);
			}
			$stock[$item_id] = $this->ipsclass->cache['market_items'][$item_id]['stock'];
		}
		$items_to_pick = array_merge(array(),$items_to_pick);
		
		$total = 0;
		$time = time();
		$items_got = array();
		$got = array();
		
		foreach($inventory_ids as $inv_id)
		{
			if($this->ipsclass->numbers->random_number(0,100) <= $this->item_config['percent'] || count($items_to_pick) == 0)
			{
				$getting = $this->ipsclass->numbers->random_number($this->item_config['lowest'],$this->item_config['highest']);
				$total = $this->ipsclass->numbers->add($total,$getting);
				$this->ipsclass->market_class->record_reward($getting);
				$this->_write_log($inv_id, "Got ".$this->ipsclass->vars['market_prefix'].$getting.$this->ipsclass->vars['market_suffix']." from mystery item");
				$got[] = $this->ipsclass->vars['market_prefix'].$getting.$this->ipsclass->vars['market_suffix'];
			}
			else
			{
				$key = $this->ipsclass->numbers->random_number(0,count($items_to_pick)-1);
				
				$inv_item = array(
									'member_id' => $this->ipsclass->member['id'],
									'item' => $items_to_pick[$key],
									'price_bought' => 0,
									'resell_price' => 0,
									'time_bought' => $time,
									'settings' => '',
								);
				$this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($inv_item);
				
				$this->ipsclass->DB->do_insert('market_inventory',$inv_item);
				$items_got[$items_to_pick[$key]]++;
				
				$this->_write_log($inv_id, "Got item ".$this->ipsclass->cache['market_items'][$items_to_pick[$key]]['item_name']." from mystery item");
				$got[] = $this->ipsclass->cache['market_items'][$items_to_pick[$key]]['item_name'];
				
				if($this->item_config['deduct'])
				{
					$stock[$items_to_pick[$key]] = $stock[$items_to_pick[$key]] <= 0 ? $stock[$items_to_pick[$key]] : $this->ipsclass->numbers->subtract($stock[$items_to_pick[$key]],1);
					if($stock[$items_to_pick[$key]] == 0)
					{
						unset($items_to_pick[$key]);
						$items_to_pick = array_merge(array(),$items_to_pick);
					}
				}
			}
		}
		
		if($this->item_config['deduct'])
		{
			foreach($items_got as $item_id => $i)
			{
				if($this->ipsclass->cache['market_items'][$item_id]['restock'] == 1 || $this->ipsclass->cache['market_items'][$item_id]['restock'] == 2)
				{
					if($this->ipsclass->numbers->subtract($this->ipsclass->cache['market_items'][$item_id]['stock'],$this->ipsclass->cache['market_items'][$item_id]['restock_when']) <= 0)
					{
						$this->ipsclass->cache['market_items'][$item_id]['restock_time'] = $this->ipsclass->market_class->next_restock_time($item_id);
					}
					else
					{
						$this->ipsclass->cache['market_items'][$item_id]['restock_time'] = 0;
					}
				}
				$this->ipsclass->DB->do_update('market_items',array(
																		'stock' => $stock[$item_id],
																		'restock_time' => $this->ipsclass->cache['market_items'][$item_id]['restock_time'],
												),"item_id=".$item_id);
			}
			
			$this->ipsclass->market_class->cache_items();
		}
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$total)
				),"id=".$this->ipsclass->member['id']);
		
		$this->delete_items($inventory_ids);
		
		$this->ipsclass->lang['got_mystery'] = str_replace("<#STUFF#>",implode(", ",$got),$this->ipsclass->lang['got_mystery']);
		$this->ipsclass->market_class->redirect('got_mystery');
	}
}
?>